import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_douban/model/store_category_model.dart';

class StoreCountModel with ChangeNotifier {
  /// 加入购物车数量
  int _count;

  /// 加入购物车数组
  List<StoreCategoryModel> storeList = [];

  /// 结算总价钱
  double? _allPrice = 0;

  /// 结算数组
  List<StoreCategoryModel>? payList = [];
  StoreCountModel(this._count, this.storeList);

  /// 加入购物车
  void add(StoreCategoryModel model) {
    if (model.r == null || model.r! == 0) {
      model.r = 1;
    }
    _count++;
    bool isRepetition = false;

    /// 判断是否是相同的商品
    for (int i = 0; i < storeList.length; i++) {
      StoreCategoryModel categoryModel = storeList[i];
      if (model.id! == categoryModel.id!) {
        model.r = model.r! + 1;
        isRepetition = true;
      }
    }
    if (isRepetition == false) {
      storeList.add(model);
    }

    if(model.is_select == true){
      _allPrice = 0;
      for (int i = 0; i < storeList.length; i++) {
        StoreCategoryModel categoryModel = storeList[i];
        if(categoryModel.is_select == true){
          _allPrice = _allPrice! +
              (NumUtil.getNumByValueDouble(
                  double.parse(categoryModel.p!) * categoryModel.r!, 2))!;
        }
      }
    }

    notifyListeners();
  }

  /// 从购物车中删除
  void remove(StoreCategoryModel model) {
    _count--;
    bool isRepetition = false;

    /// 判断是否是相同的商品
    for (int i = 0; i < storeList.length; i++) {
      StoreCategoryModel categoryModel = storeList[i];
      if (model.id! == categoryModel.id!) {
        model.r = model.r! - 1;
        if (model.r! > 0) {
          isRepetition = true;
        } else {
          isRepetition = false;
        }
      }
    }
    if (isRepetition == false) {
      storeList.remove(model);
    }

    if(model.is_select == true){
      _allPrice = 0;
      for (int i = 0; i < storeList.length; i++) {
        StoreCategoryModel categoryModel = storeList[i];
        if(categoryModel.is_select == true){
          _allPrice = _allPrice! +
              (NumUtil.getNumByValueDouble(
                  double.parse(categoryModel.p!) * categoryModel.r!, 2))!;
        }
      }
    }
    notifyListeners();
  }

  /// 结算 取消结算
  void pay(StoreCategoryModel model) {
    _allPrice = 0;
    if (model.is_select == true) {
      payList!.add(model);
    } else {
      payList!.remove(model);
    }
    for (int i = 0; i < payList!.length; i++) {
      StoreCategoryModel categoryModel = payList![i];
      _allPrice = _allPrice! +
          (NumUtil.getNumByValueDouble(
              double.parse(categoryModel.p!) * categoryModel.r!, 2))!;
    }
    notifyListeners();
  }

  /// 全选
  void selectAll(bool is_select) {
    _allPrice = 0;
    /// 选中状态
    /// 取消选中状态
    for (int i = 0; i < storeList.length; i++) {
      StoreCategoryModel categoryModel = storeList[i];
      categoryModel.is_select = is_select;
      is_select == true ? _allPrice = _allPrice! +
          (NumUtil.getNumByValueDouble(
              double.parse(categoryModel.p!) * categoryModel.r!, 2))! : 0;
    }
    notifyListeners();
  }

  /// 获取购物车总数量
  int get count => _count;

  /// 获取结算总价钱
  double? get allPrice => _allPrice;
}
